package club.yuit.desktop.ui.consumer;

import club.yuit.desktop.model.ConsumerContext;
import club.yuit.rmq.service.ConsumerService;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;

import javax.swing.*;
import java.awt.*;

/**
 * @author: yuit
 * @date: 2023/12/12 13:43
 */
public class ConfigConsumerDialog  {


    private JOptionPane optionPane;
    private String topic;
    private JTextField groupField;
    private JComboBox<String> filterType;
    private JTextField filterExpression;

    private Component parent;

    public ConfigConsumerDialog(String topic, Component parent) {
        this.topic = topic;
        this.parent = parent;
        optionPane = new JOptionPane();
        JButton ok = new JButton("确定");
        JButton cancel = new JButton("取消");
        JTextField topicField = new JTextField(topic);
        topicField.setEnabled(false);
        groupField = new JTextField();
        filterExpression = new JTextField();
        filterType = new JComboBox<>();
        filterType.addItem("TAG");
        filterType.addItem("SQL92");
        optionPane.setMessage(
                new Object[]{
                        new JLabel("topic:")
                        ,topicField
                        ,new JLabel("Group:")
                        ,groupField,
                        new JLabel("FilterType:")
                       ,filterType
                       ,new JLabel("FilterExpression:")
                       ,filterExpression
                }
        );

        optionPane.setOptions(new Object[]{
                ok,cancel
        });


        ok.addActionListener(e -> {
            ConsumerContext context = new ConsumerContext();
            context.setTopic(topic);
            context.setMessageGroup(groupField.getText());
            String filterTypeSelectedItem = (String) filterType.getSelectedItem();
            context.setFilterExpressionType(filterTypeSelectedItem);
            String expressionText = filterExpression.getText();
            expressionText = switch (filterTypeSelectedItem){
                case "TAG" -> {
                    if (StringUtils.isBlank(expressionText)){
                        yield "*";
                    }
                    yield expressionText;
                }
                case "SQL92" -> {
                    if (StringUtils.isBlank(expressionText)){
                        yield "";
                    }
                    yield expressionText;
                }

                default -> "*";
            };
            context.setFilterExpression(expressionText);

            context.setConsumerService(new ConsumerService());

            ConsumerUI  cui = new ConsumerUI(context,parent);
            cui.setVisible(true);
            disposeParent(ok);
        });

        cancel.addActionListener(e -> disposeParent(cancel));
    }


    public void showDialog(){
        JOptionPane.showOptionDialog(
                parent,
                optionPane.getMessage(),
                "配置",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                optionPane.getIcon(),
                optionPane.getOptions(),
                optionPane.getInitialValue()
        );
    }


    /**
     * 查找当前组件的父组件并关闭
     * @param c 当前组件
     */
    public void disposeParent(JComponent c) {
        Window windowAncestor = SwingUtilities.getWindowAncestor(c);
        if (windowAncestor!= null) {
            windowAncestor.dispose();
        }
    }
}
